<template>
  <div id="change">
    <a-steps :current="current">
      <a-step v-for="item in steps" :key="item.title" :title="item.title" />
    </a-steps>

    <div class="steps-content">
      <socialPersonInfoVO v-if="current == 0" :result="result" @SociaFormModel="SociaFormModel" @FamilyTabledata="FamilyTabledata" @SeizedTabledata="SeizedTabledata" :huancun="huancun" ref="socialPersonInfoVO"></socialPersonInfoVO>
      <Open v-if="current == 1" :sjskPersonInfoVO="sjskPersonInfoVO" @opendata="opendata" :huancun="huancun" ref="Open" ></Open>
      <Agreement v-if="current == 2" :OpenForm="OpenForm" @agreedata="agreedata" :huancun="huancun" ref="Agreement" ></Agreement>
      <helpPlanVO v-if="current == 3" @helpFormModel="helpFormModel" @PlanTabledata="PlanTabledata" @WorkGroupTabledata="WorkGroupTabledata" :huancun="huancun" ref="helpPlanVO"></helpPlanVO>
      <Control v-if="current == 4" :sjskPersonInfoVO="sjskPersonInfoVO" @ControlForm="ControlForm" :huancun="huancun" ref="Control" ></Control>
    </div>

    <div class="steps-action">
      <a-button v-if="current > 0"  @click="prev">
        上一步
      </a-button>

      <a-button v-if="current < steps.length - 1" type="primary" @click="next" style="margin-left: 8px">
        下一步
      </a-button>
      <!-- @click="$message.success('Processing complete!')" -->
      <a-button
        v-if="current == steps.length - 1"
        type="primary"
        
        @click="reporttion()"
        style="margin-left: 8px"
      >
        完成
      </a-button>
  
      <a-button style="margin-left: 8px" @click="save" type="primary" ghost>
        保存并退出
      </a-button>
    </div>

  </div>
</template>
<script>
// import Form from './Form.vue'
// import {details} from '@/api/sjsk/reportList'
import Open from './Open.vue'
import Agreement from './Agreement.vue'
// import Archives from './Archives.vue'
import Control from './Control.vue'
import helpPlanVO from './helpPlanVO.vue'
import socialPersonInfoVO from './socialPersonInfoVO.vue'
import {reportRegistration,addReportRegistration,viewReportRegistration,details} from '@/api/sjsk/reportList'
export default {
  async mounted () {
    // setTimeout(this.getsave,3000)
    // await this.getsave()
    await this.getsave()
    this.refreshTable()
    
  },
  components:{ Open,Agreement,Control,helpPlanVO,socialPersonInfoVO },
  data() {
    return {
      reportqurey:{
        addScheduleDTO:{},
        sjskPersonAddVO:{},
        workGroupAndPersonDTO:{},
        personId:''
      },
      huancun:{},
      caseNo:'',
      addScheduleDTO:{},
      sjskPersonInfoVO:{},
      baseFamilyList:[],
      sjskOtherCriminalRecordList:[],
      OpenForm:{},
      sjskReportRegistration:{},
      sjskLocationInfo:{},
      sjskTeamerList:[],
      actionPlans:[],
      mstDTO:{},
      id:this.$route.query.id,
      result:{},
      current: 0,
      helpform:{},
      steps: [
        {
          title: '社区戒毒/社区康复人员登记表',
        },
        {
          title: '椰风送暖开户',
        },
        {
          title: '签订协议',
        },
        {
          title: '完善人员档案',
        },
        {
          title: '管控服务设置',
        },
      ],
    };
  },
  methods: {
    refreshTable() {
      // this.loading = true
      details({id:this.id}).then((res) => {
        if (res.success) {
          // this.model = res.result ? res.result : {}
          // delete this.model.firstDrugTime
          this.result = res.result
          this.loading = false
          console.log(this.result);
        } else {
          // this.pageData = {}
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },
    next() {
      if(this.current == 0){
        this.$nextTick(() => {
          this.$refs.socialPersonInfoVO.$refs.ruleForm.validate((valid) => {
            if (valid) {
              if(this.baseFamilyList.length == 0){
                this.$message.warn('家属栏请至少新增一条数据！')
                return
              }
              else if(this.sjskOtherCriminalRecordList.length == 0){
                this.$message.warn('查获情况及前科请至少新增一条数据！')
                return
              }
              else{
                this.current++;
              }
            } 
            else { return false;}
          })
        })
      }
      if (this.current == 1 ) {
        this.$nextTick(() => {
          this.$refs.Open.$refs.ruleForm.validate((valid) => {
            if (valid) {
              this.current++;
            } else {
            return false;
          }
          })
        })
      }
      if (this.current == 2 ) {
        this.$nextTick(() => {
          this.$refs.Agreement.$refs.ruleForm.validate((valid) => {
            if (valid) {
              this.current++;
            } else {
            return false;
          }
          })
        })
      }
      else {
        this.$nextTick(() => {
          this.$refs.helpPlanVO.$refs.ruleForm.validate((valid) => {
            if (valid) {
              if(this.sjskTeamerList.length == 0){
                this.$message.warn('工作小组请至少新增一条数据！')
                return
              }
              else if(this.actionPlans.length == 0){
                this.$message.warn('行动计划表请至少新增一条数据！')
                return
              }
              else{
                this.current++;
              }
            } 
            else { return false;}
          })
        })
      }
    },
    prev() {
      this.current--;
    },
    save(){
      addReportRegistration({
        caseNo:this.caseNo,
        personId:this.reportqurey.personId,
        json:JSON.stringify(this.reportqurey)
      }).then((res) => {
        if (res.success) {
          console.log(res);
          // this.getsave()
          this.loading = false
        } else {
          // this.pageData = {}
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
        this.$router.push({
        path: "/reportList/index",
      })
    },
    getsave(){
      viewReportRegistration({
        personId:this.id,
      }).then((res) => {
        if (res.success) {
          // console.log(res);
          this.huancun = JSON.parse(res.result.json)
          // console.log(JSON.parse(res.result.json));
          console.log(this.huancun);
          this.loading = false
        } else {
          // this.pageData = {}
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },
  SociaFormModel(val){
    this.sjskPersonInfoVO = val
    this.reportqurey.personId = val.id
    this.caseNo = val.caseNo
   },
  FamilyTabledata(val){
    this.baseFamilyList = val
   },
   SeizedTabledata(val){
    this.sjskOtherCriminalRecordList = val
   },
   opendata(val){
    // let {name,password} = val
    // this.OpenForm = {
    //   username:name,
    //   password:password
    // }
    this.OpenForm = val
    this.OpenForm.username = val.name
   },
   agreedata(val){
    this.sjskReportRegistration = val
    
   },

   helpFormModel(val){
    this.helpform = val
    let {liveHouseMap,livePositionMap,remark,planType,planContent,risk,reservePlan,budget,inputResourses} = val
    this.sjskLocationInfo = {
          liveHouseMap:liveHouseMap,
          livePositionMap:livePositionMap,
          remark:remark
        }
    this.addScheduleDTO = {
      planType:planType,
      planContent:planContent,
      risk:risk,
      reservePlan:reservePlan,
      budget:budget,
      inputResourses:inputResourses
    }

   },
   PlanTabledata(val){
    this.actionPlans = val
   },
   WorkGroupTabledata(val){
    this.sjskTeamerList = val
   },

   ControlForm(val){
    this.mstDTO = val
    
   },
   reporttion(){
    this.$nextTick(() => {
          this.$refs.Control.$refs.ruleForm.validate((valid) => {
            if (valid) {
              reportRegistration(this.reportqurey).then((res) => {
                  if (res.success) {
                    console.log(res);
                    this.$message.success(res.message)
                    this.loading = false
                  } else {
                    // this.pageData = {}
                    this.$message.warn(res.message)
                    this.loading = false
                  }
                })
                  .catch((e) => {
                    this.loading = false
                    this.$message.warn('请求失败！')
                  })
            } else {
            return false;
          }
          })
        })
   }
  },
  watch:{
    helpform:{
      deep:true,
      handler(val){
        this.reportqurey.helpform = val
      }
    },
    OpenForm:{
      deep:true,
      handler(val){
        this.reportqurey.OpenForm = val
        this.reportqurey.sjskPersonAddVO.sjskPersonInfoVO.username = val.username
        this.reportqurey.sjskPersonAddVO.sjskPersonInfoVO.password = val.password
      }
    },
    addScheduleDTO:{
      deep:true,
      handler(val){
        this.reportqurey.addScheduleDTO = val
        this.reportqurey.addScheduleDTO.actionPlans = this.actionPlans
      }
    },
    sjskPersonInfoVO:{
      deep:true,
      handler(val){
        this.reportqurey.sjskPersonAddVO.sjskPersonInfoVO = val
        this.reportqurey.sjskPersonAddVO.sjskPersonInfoVO.policeReportsDate = val.reportTime
      }
    },
    baseFamilyList:{
      deep:true,
      handler(val){
        this.reportqurey.sjskPersonAddVO.baseFamilyList = val
      }
    },
    sjskLocationInfo:{
      deep:true,
      handler(val){
        this.reportqurey.sjskPersonAddVO.sjskLocationInfo = val
        this.reportqurey.sjskPersonAddVO.sjskLocationInfo.policeReportsDate = val.reportTime
      }
    },
    sjskOtherCriminalRecordList:{
      deep:true,
      handler(val){
        this.reportqurey.sjskPersonAddVO.sjskOtherCriminalRecordList = val
      }
    },

    sjskReportRegistration:{
      deep:true,
      handler(val){
        this.reportqurey.sjskReportRegistration = val
      }
    },
    sjskTeamerList:{
      deep:true,
      handler(val){
        this.reportqurey.workGroupAndPersonDTO.sjskTeamerList = val
      }
    },
    mstDTO:{
      deep:true,
      handler(val){
        this.reportqurey.mstDTO = val
      }
    },

    actionPlans:{
      deep:true,
      handler(val){
        this.reportqurey.addScheduleDTO.actionPlans = val
      }
    },
  }
 
};
</script>
<style lang="less" scoped>
.steps-content {
  margin-top: 16px;
  // border: 1px dashed #e9e9e9;
  border-radius: 6px;
  // background-color: white;
  min-height: 200px;
  // text-align: center;
  padding-top: 40px;
}

.steps-action {
  margin-top: 24px;
  // background-color: white;
  margin-left: 120px;
}
.ant-steps{
  // background-color: white;
  width: 80%;
  margin-left:10%;
  margin-top:20px
}

#change{
  background-color: white;
  padding:40px
}
</style>